<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="ip地址" prop="ip">
        <el-input
          v-model="queryParams.ip"
          placeholder="请输入ip地址"
          clearable
          size="mini"
          style="width: 120px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人" prop="adminName">
        <el-input
          v-model="queryParams.adminName"
          placeholder="请输入操作人"
          clearable
          size="mini"
          style="width: 120px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作类型" prop="type">
        <el-select v-model="queryParams.type" size="mini" style="width: 120px" clearable>
          <el-option label="新增或修改" value="新增或修改"></el-option>
          <el-option label="新增" value="新增"></el-option>
          <el-option label="修改" value="修改"></el-option>
          <el-option label="删除" value="删除"></el-option>
          <el-option label="查询" value="查询"></el-option>
          <el-option label="其他" value="其他"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="请求类型" prop="requestMethod">
        <el-select v-model="queryParams.requestMethod" size="mini" style="width: 120px" clearable>
          <el-option label="GET" value="GET"></el-option>
          <el-option label="POST" value="POST"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="操作结果" prop="status">
        <el-select v-model="queryParams.status" size="mini" style="width: 120px" clearable>
          <el-option label="成功" :value=0></el-option>
          <el-option label="失败" :value=1></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="操作时间" prop="beginTime">
        <el-date-picker
          v-model="queryParams.beginTime"
          size="mini"
          type="datetime"
          placeholder="选择日期时间"
          align="right"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="-"  prop="endTime">
        <el-date-picker
          v-model="queryParams.endTime"
          size="mini"
          type="datetime"
          placeholder="选择日期时间"
          align="right"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="logList" >
      <el-table-column label="IP地址" prop="ip" align="center"/>
      <el-table-column label="操作人" prop="adminName"  align="center"/>
      <el-table-column label="操作事件" prop="message"  :show-overflow-tooltip="true"  align="center"/>
      <el-table-column label="操作类型" prop="type"  align="center"/>
      <el-table-column label="操作结果"  align="center" >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status==0">成功</el-tag>
          <el-tag type="danger" v-else>失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="请求类型" prop="requestMethod"  align="center"/>
      <el-table-column label="操作时间" align="center" prop="createTime" ></el-table-column>
      <el-table-column label="错误信息" prop="errorMsg" align="center"  :show-overflow-tooltip="true"></el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>

</template>

<script>
import { getLogList } from '@/api/system/logs'

export default {
  name: "Log",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      logList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        adminName:undefined,
        requestMethod:undefined,
        ip:undefined,
        status:undefined,
        type:undefined,
        beginTime:undefined,
        endTime:undefined
      },
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.beginTime){
        this.queryParams.beginTime =  this.$moment(this.queryParams.beginTime).format('YYYY-MM-DD HH:mm:ss')
      }
      if (this.queryParams.endTime){
        this.queryParams.endTime =  this.$moment(this.queryParams.endTime).format('YYYY-MM-DD HH:mm:ss')
      }
      getLogList(this.queryParams).then(res => {
          this.logList = res.page.records;
          this.total = res.page.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>
